package com.cyy.content.service.jobhandler;

import com.cyy.base.exception.LearningException;
import com.cyy.messagesdk.model.po.MqMessage;
import com.cyy.messagesdk.service.MessageProcessAbstract;
import com.cyy.messagesdk.service.MqMessageService;
import com.cyy.messagesdk.model.po.MqMessage;
import com.cyy.messagesdk.service.MessageProcessAbstract;
import com.cyy.messagesdk.service.MqMessageService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author cyy
 * @date 2025/2/22
 * @description CoursePublishTask
 */
@Slf4j
@Component
@ComponentScan(basePackages = {"com.cyy.messagesdk.service","com.cyy.content.service"})
public class CoursePublishTask extends MessageProcessAbstract {


    @XxlJob("coursePublishJobHandler")
    public void coursePublishJobHandler() throws Exception{
        //分片参数
        int shardIndex = XxlJobHelper.getShardIndex();
        int shardTotal = XxlJobHelper.getShardTotal();
        //调用抽象类的方法执行任务
        process(shardIndex, shardTotal, "course_publish", 30, 60);

    }

    //执行课程发布任务的逻辑
    @Override
    public boolean execute(MqMessage mqMessage) {
        //从mqMessage拿到课程id
        Long courseId = Long.parseLong(mqMessage.getBusinessKey1());
        //课程静态化上传到minio（课程静态化）
        generateCourseHtml(mqMessage, courseId);
        //向 Elasticsearch写索引数据(课程索引)
        saveCourseIndex(mqMessage, courseId);
        //向redis写缓存（课程缓存）
        saveCourseCache(mqMessage, courseId);
        //返回true表示任务完成
        return true;
    }

    private void saveCourseCache(MqMessage mqMessage, Long courseId) {
        log.debug("将课程信息缓存至redis,课程id:{}",courseId);

        try {
            TimeUnit.SECONDS.sleep(10);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //生成课程静态化界面上传至文件系统
    private void generateCourseHtml(MqMessage mqMessage, Long courseId){
        //消息id
        Long taskId = mqMessage.getId();
        MqMessageService mqMessageService = this.getMqMessageService();
        //任务幂等性处理
        //取出该阶段的执行状态
        int stageOne = mqMessageService.getStageOne(taskId);
        if(stageOne > 0){
            log.debug("课程静态化任务完成，无需处理...");
            return;
        }
        try {
            TimeUnit.SECONDS.sleep(10);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        //开始进行课程静态化
        mqMessageService.completedStageOne(taskId);

    }
    //保存课程索引信息
    private void saveCourseIndex(MqMessage mqMessage, Long courseId){
        //任务id
        Long taskId = mqMessage.getId();
        MqMessageService mqMessageService = this.getMqMessageService();
        //任务幂等性处理
        int stageTwo = mqMessageService.getStageTwo(taskId);
        if(stageTwo > 0){
            log.debug("保存课程索引信息，无需处理...");
            return;
        }
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }
}
